#!/usr/bin/perl -w
package mail;
use strict;
use Net::SMTP_auth;

no warnings('once');  
    #这一句，可以避免出现由于变量只使用一次出现的 use only once 警告 g
require '../../conf/config.pl';
    #包含配置文件
require 'data.pl';

our %data_frm = %data::db ;

# print $data_frm{'html'};

$ENV{TZ} = 'Asia/Shanghai'; # 设置时区

our %db_conf = %conf::db_conf ;  
our %em_conf = %conf::em_conf ;
 
#smtp邮件服务器和端口
my $smtpHost = $em_conf{'smtpHost'};
my $smtpPort = $em_conf{'smtpPort'};
my $sslPort = $em_conf{'sslPort'};
 
#smtp服务器认证用户名密码(就是你登陆邮箱的时候的用户名和密码)
my $username = $em_conf{'username'};
my $passowrd = $em_conf{'password'};
 
#邮件来自哪儿，要去哪儿,邮件标题
my $from = $em_conf{'from'};
my $subject = "【Altiris Solutions】：财务报表 - 月报表 ";
#my $to="meansonw\@gmail.com";
my $to = "$data_frm{'to'}";

#设置邮件header
my $header = << "MAILHEADER";
Mime-Version:1.0
Content-Type:text/html;charset="UTF-8"
Content-Trensfer-Encoding:7bit 
Subject: $subject
From:$from
MAILHEADER

#设置邮件内容
my $message = << "MAILBODY";
$data_frm{'html'}

MAILBODY
 
#获得邮件域名部分，用于连接的时候表名身份
my @helo = split /\@/,$from;
 
#连接smtp服务器，明文/SSL/TLS三种方式，根据你使用的SMTP支持情况选择一种
#普通方式，通信过程不加密
my $smtp = Net::SMTP_auth->new(
                "$smtpHost:$smtpPort",
                Hello   => $helo[1],
                Timeout => 30
                ) or die("Error:连接到$smtpHost失败！");
$smtp->auth('LOGIN',$username,$passowrd) or die("Error:认证失败！");
 


#发送邮件
$smtp->mail($from);
#$smtp->to($to);
# Multi Recipients
my @recipients= split /,/,$to;
my $n = 0;
while ($recipients[$n]) {
 $smtp->to($recipients[$n]);
 $n++;
}
$smtp->data();
$smtp->datasend($header);
$smtp->datasend("To:$to\n");
$smtp->datasend("\n");
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit();
 
#print "OK";
exit 0;
